package com.dp.service.impl3;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.service.impl3.BaseImpl;
import com.dp.util.KeyGenerator;
import com.dp.vo.TMainmenus;

@Service
@Transactional
public class MainmenusServiceImpl3 extends BaseImpl {

	/**
	 * 查询
	 * 
	 * @return
	 */
	public Long findMainmenusCount(TMainmenus mainmenus,JsonPage jpage) {
			StringBuffer hql = new StringBuffer("SELECT count(a) FROM TMainmenus a where 1=1 ");
			if (mainmenus != null) {
				if (mainmenus.getMainmenuName() != null
						&& !"".equals(mainmenus.getMainmenuName())) {
					hql.append(" and a.mainmenuName like '%" + mainmenus.getMainmenuName() + "%'");
				}
			}
			Object obj = getSingleResult(hql.toString());
			return obj == null ? 0L : (Long) obj;
	}

	/**
	 * 查询,分页
	 * 
	 * @param clientua
	 * @return
	 */
	public List<TMainmenus> findMainmenus(TMainmenus mainmenus,JsonPage jpage, int firstrow, int maxrow) {
		StringBuffer hql = new StringBuffer("SELECT a FROM TMainmenus a where 1=1 ");
		if (mainmenus != null) {
			if (mainmenus.getMainmenuName() != null
					&& !"".equals(mainmenus.getMainmenuName())) {
				hql.append(" and a.mainmenuName like '%" + mainmenus.getMainmenuName() + "%'");
			}
		}
		if(jpage!=null){
			if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
				hql.append(" order by a."+jpage.getSidx());
				hql.append(" "+jpage.getSord());
			}
		}
		return getQueryList(hql.toString(), firstrow, maxrow);
	}

	/*public TMainmenus findAdById(String mainmenuid) {
		String hql = "SELECT a FROM TMainmenus a where 1=1 ";
		if (mainmenuid != null && !"".equals(mainmenuid)) {
			hql += " and a.mainmenuId = '" + mainmenuid + "'";
		}
		return (TMainmenus)getSingleResult(hql);
	}*/

	public int update(TMainmenus mainmenus) {
		String hql = "UPDATE TMainmenus SET mainmenuName='" + mainmenus.getMainmenuName()
				+ "',isUse='" + mainmenus.getIsUse()
				+ "' where mainmenuId='" + mainmenus.getMainmenuId()+"'";
		return super.updateHql(hql);
	}

	public Object save(TMainmenus mainmenus) {
		String mainmenuId = com.dp.util.BaseBean.ObjectToString(KeyGenerator.getInstance().getNextKeyValue("t_mainmenus"));	//获取主键（非自增）
		mainmenus.setMainmenuId(mainmenuId);
		return super.save(mainmenus);
	}

	/*public int checkMainmenusName(String mainmenuName, String old_mainmenuName) {
		String hql = "SELECT a FROM TMainmenus a where 1=1 ";
		if (mainmenuName != null && !"".equals(mainmenuName)) {
			hql += " and a.mainmenuName = '" + mainmenuName + "'";
		}
		if (old_mainmenuName != null && !"".equals(old_mainmenuName)) {
			hql += " and a.mainmenuName != '" + old_mainmenuName + "'";
		}
		return getSingleResult(hql)==null ? 0 : 1;
	}*/

	public TMainmenus checkMainmenus_mainmenuName(String mainmenuName) {
		String hql = "SELECT a FROM TMainmenus a where 1=1 ";
		if (mainmenuName != null && !"".equals(mainmenuName)) {
			hql += " and a.mainmenuName = '" + mainmenuName + "'";
		}
		return (TMainmenus) getSingleResult(hql);
	}

	/**
	 * Delete NewsBlacklist Entity
	 * 
	 * @param urlB
	 * @return boolean b
	 */
	public boolean deleteMainmenusById(String mainmenuId) {
		String jpql = "DELETE TMainmenus n WHERE n.mainmenuId='" + mainmenuId + "'";
		return super.updateHql(jpql) > 0;
	}

}
